load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//xla:xla.default.bzl", "xla_cc_test")
load("//xla/tsl:tsl.bzl", "if_google")
load("//xla/tsl/platform:build_config.bzl", "tf_proto_library")

package(
    # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
    default_visibility = [":friends"],
    licenses = ["notice"],
)

package_group(
    name = "friends",
    includes = [
        "//xla:friends",
    ],
)

cc_library(
    name = "codegen_backend",
    hdrs = ["codegen_backend.h"],
    deps = [
        "//xla/hlo/ir:hlo",
        "//xla/service:executable",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_protobuf//:any_cc_proto",
    ],
)

cc_library(
    name = "autotuner",
    srcs = ["autotuner.cc"],
    hdrs = ["autotuner.h"],
    deps = [
        ":autotuner_cache_interface",
        ":codegen_backend",
        ":profiler",
        "//xla:autotune_results_proto_cc",
        "//xla:autotuning_proto_cc",
        "//xla/hlo/ir:hlo",
        "//xla/service:executable",
        "//xla/service:shaped_buffer",
        "//xla/stream_executor:device_description",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:errors",
        "//xla/tsl/platform:status",
        "//xla/tsl/platform:statusor",
        "//xla/tsl/util/proto:proto_utils",
        "@com_google_absl//absl/base:no_destructor",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/functional:function_ref",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/time",
        "@tsl//tsl/platform:blocking_counter",
        "@tsl//tsl/platform:fingerprint",
        "@tsl//tsl/platform:path",
        "@tsl//tsl/platform:protobuf",
    ] + if_google(["@com_google_protobuf//:any_cc_proto"]),
)

xla_cc_test(
    name = "autotuner_test",
    srcs = ["autotuner_test.cc"],
    deps = [
        ":autotuner",
        ":autotuner_cache_interface",
        ":codegen_backend",
        ":profiler",
        "//xla:literal_util",
        "//xla:shape_util",
        "//xla/hlo/ir:hlo",
        "//xla/hlo/testlib:hlo_hardware_independent_test_base",
        "//xla/service:executable",
        "//xla/service:shaped_buffer",
        "//xla/service/gpu:backend_configs_cc",
        "//xla/stream_executor:device_description",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:statusor",
        "//xla/tsl/platform:test",
        "//xla/tsl/util/proto:proto_matchers",
        "//xla/tsl/util/proto:proto_utils",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:status_matchers",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:optional",
        "@com_google_googletest//:gtest_main",
        "@tsl//tsl/platform:path",
        "@tsl//tsl/platform:protobuf",
        "@tsl//tsl/platform:test",
    ] + if_google(["@com_google_protobuf//:any_cc_proto"]),
)

tf_proto_library(
    name = "autotuner_cache_proto",
    srcs = ["autotuner_cache.proto"],
    deps = if_google(["@com_google_protobuf//:any"]),
)

cc_library(
    name = "profiler",
    hdrs = ["profiler.h"],
    deps = [
        "//xla/service:executable",
        "//xla/service:shaped_buffer",
        "//xla/tsl/platform:statusor",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/time",
    ],
)

cc_library(
    name = "autotuner_cache_interface",
    hdrs = ["autotuner_cache_interface.h"],
    deps = [
        ":autotuner_cache_proto_cc",
        "//xla/hlo/ir:hlo",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings:string_view",
    ],
)

cc_library(
    name = "file_based_autotuner_cache",
    srcs = ["file_based_autotuner_cache.cc"],
    hdrs = ["file_based_autotuner_cache.h"],
    deps = [
        ":autotuner_cache_interface",
        ":autotuner_cache_proto_cc",
        "//xla/hlo/ir:hlo",
        "//xla/stream_executor:device_description",
        "//xla/stream_executor/cuda:cuda_compute_capability",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:errors",
        "//xla/tsl/platform:statusor",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:string_view",
        "@com_google_absl//absl/synchronization",
        "@llvm-project//llvm:Support",
        "@tsl//tsl/platform:base64",
        "@tsl//tsl/platform:path",
        "@tsl//tsl/platform:protobuf",
    ],
)

xla_cc_test(
    name = "file_based_autotuner_cache_test",
    srcs = ["file_based_autotuner_cache_test.cc"],
    deps = [
        ":autotuner_cache_interface",
        ":autotuner_cache_proto_cc",
        ":file_based_autotuner_cache",
        "//xla:literal_util",
        "//xla/hlo/ir:hlo",
        "//xla/stream_executor:device_description",
        "//xla/stream_executor/cuda:cuda_compute_capability",
        "//xla/tsl/lib/core:status_test_util",
        "//xla/tsl/platform:env",
        "//xla/tsl/platform:statusor",
        "//xla/tsl/util/proto:proto_matchers",
        "@com_google_absl//absl/status",
        "@com_google_googletest//:gtest_main",
        "@tsl//tsl/platform:path",
    ] + if_google(["@com_google_protobuf//:any_cc_proto"]),
)
